package com.definesys.smecrailtransit.utils;

import android.content.Context;
import android.widget.Toast;

import com.definesys.smecrailtransit.R;

import es.dmoral.toasty.Toasty;

/**
 * Created by xupeizuo on 2017/8/1.
 * 避免同样的信息多次触发重复弹出的问题 2017/8/02/
 * @author xupeizuo
 */

public final class ToastyUtils {

    public static Toast mToast=null;
    public static Toast errorToast=null;
    public static Toast successToast=null;
    public static Toast waringToast=null;
    public static Toast customToast=null;

    private static String oldMsg;
    private static long oneTime = 0;
    private static long twoTime = 0;

    public static void showNormalToast(Context context,String s){
        if(mToast == null){
            oldMsg=s;
            mToast=Toasty.normal(context,s);
            mToast.show();
            oneTime = System.currentTimeMillis();
        }else {
            twoTime = System.currentTimeMillis();
            if (s.equals(oldMsg)) {
                if (twoTime - oneTime > Toast.LENGTH_SHORT) {
                    mToast.show();
                }
            } else {
                oldMsg = s;
                mToast=Toasty.normal(context,s);
                mToast.show();
            }
            oneTime = twoTime;
        }
    }

    public static void showErrorToast(Context context,String s){
        if(errorToast == null){
            oldMsg=s;
            errorToast=Toasty.normal(context, s,context.getResources().getDrawable(R.drawable.smec_error));
            errorToast.show();
            oneTime = System.currentTimeMillis();
        }else {
            twoTime = System.currentTimeMillis();
            if (s.equals(oldMsg)) {
                if (twoTime - oneTime > Toast.LENGTH_SHORT) {
                    errorToast.show();
                }
            } else {
                oldMsg = s;
                errorToast=Toasty.normal(context, s,context.getResources().getDrawable(R.drawable.smec_error));
                errorToast.show();
            }
            oneTime = twoTime;
        }
    }

    public static void showWaringToast(Context context,String s){
        if(waringToast == null){
            oldMsg=s;
            waringToast=Toasty.normal(context, s,context.getResources().getDrawable(R.drawable.smec_warring));
            waringToast.show();
            oneTime = System.currentTimeMillis();
        }else {
            twoTime = System.currentTimeMillis();
            if (s.equals(oldMsg)) {
                if (twoTime - oneTime > Toast.LENGTH_SHORT) {
                    waringToast.show();
                }
            } else {
                oldMsg = s;
                waringToast=Toasty.normal(context, s,context.getResources().getDrawable(R.drawable.smec_warring));
                waringToast.show();
            }
            oneTime = twoTime;
        }
    }

    public static void showSuccessToast(Context context,String s){
        if(successToast == null){
            oldMsg=s;
            successToast=Toasty.normal(context, s,context.getResources().getDrawable(R.drawable.smec_success));
            successToast.show();
            oneTime = System.currentTimeMillis();
        }else {
            twoTime = System.currentTimeMillis();
            if (s.equals(oldMsg)) {
                if (twoTime - oneTime > Toast.LENGTH_SHORT) {
                    successToast.show();
                }
            } else {
                oldMsg = s;
                successToast=Toasty.normal(context, s,context.getResources().getDrawable(R.drawable.smec_success));
                successToast.show();
            }
            oneTime = twoTime;
        }
    }

    public static void showCustomToast(Context context,String s,int yourIconDrawable, int tintColor,int duration
            ,boolean withIcon,boolean shouldTint){
        if(customToast == null){
            oldMsg=s;
            customToast=Toasty.custom(context, s, yourIconDrawable, tintColor, duration, withIcon,
                    shouldTint);
            customToast.show();
            oneTime = System.currentTimeMillis();
        }else {
            twoTime = System.currentTimeMillis();
            if (s.equals(oldMsg)) {
                if (twoTime - oneTime > Toast.LENGTH_SHORT) {
                    customToast.show();
                }
            } else {
                oldMsg = s;
                customToast=Toasty.custom(context, s, yourIconDrawable, tintColor, duration, withIcon,
                        shouldTint);
                customToast.show();
            }
            oneTime = twoTime;
        }
    }

}
